<?php

namespace app\admin\command;

use app\common\model\Config;
use app\common\model\GoldLog;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class Distribution extends Command
{
    protected function configure()
    {
        $this->setName('Distribution')->setDescription('每日分销计算');
    }

    //30秒执行1次
    protected function execute(Input $input, Output $output)
    {
        $output->writeln('Date Crontab job start...');
        //return true;
        //log_print(date("Y-m-d H:i:s"));
        /*** 这里写计划任务列表集 START ***/
        //判断今日是否有收益,如果有今日数据则不执行
        $date = date('Y-m-d', time());
        $c = GoldLog::where(['create_at' => $date])->count();
        if ($c > 0) {
            return true;
        }
        //每日固定收益大于0,目前冻结黄金券大于0
        $userList1 = Db::name('user')->where(['fixed_gold' => ['>', 0], 'freeze_gold' => ['>', 0]])->field('id,fixed_gold,freeze_gold,parent_user_id,team_consume,total_consume')->select();
        for ($i = 0; $i < count($userList1); $i++) {
            //固定收益
            if ($userList1[$i]['fixed_gold'] > $userList1[$i]['freeze_gold']) {
                $shouyi = $userList1[$i]['freeze_gold'];
                Db::name('user')->where(['id' => $userList1[$i]['id']])->setField('freeze_gold', 0);//冻结券清零
            } else {
                $shouyi = $userList1[$i]['fixed_gold'];
                Db::name('user')->where(['id' => $userList1[$i]['id']])->setDec('freeze_gold', $shouyi);//冻结减少
            }
            Db::name('user')->where(['id' => $userList1[$i]['id']])->setInc('thaw_gold', $shouyi);//冻结券剩余增加至解冻券
            //记录日志
            GoldLog::create(['user_id' => $userList1[$i]['id'], 'gold' => $shouyi, 'memo' => '每日固定收益', 'type' => 'thaw', 'create_at' => date('Y-m-d')]);
            //直接推荐
            if ($userList1[$i]['parent_user_id']) {
                //有上级,获取下级100%收益
                $parent = Db::name('user')->field('id,freeze_gold,parent_user_id')->where(['freeze_gold' => ['>', 0]])->find($userList1[$i]['parent_user_id']);
                if ($parent) {
                    if ($userList1[$i]['fixed_gold'] <= $parent['freeze_gold']) {
                        $shouyi_p = $userList1[$i]['fixed_gold'];
                        Db::name('user')->where(['id' => $parent['id']])->setDec('freeze_gold', $shouyi_p);//冻结减少
                    } else {
                        $shouyi_p = $parent['freeze_gold'];
                        Db::name('user')->where(['id' => $parent['id']])->setField('freeze_gold', 0);//冻结券清零
                    }
                    Db::name('user')->where(['id' => $parent['id']])->setInc('thaw_gold', $shouyi_p);//冻结券剩余增加至解冻券
                    //记录日志
                    GoldLog::create(['user_id' => $parent['id'], 'gold' => $shouyi_p, 'memo' => '直接推荐奖励', 'type' => 'thaw', 'create_at' => date('Y-m-d')]);
                    //判断是否存在上上级
                    if ($parent['parent_user_id']) {
                        //有上上级,收益50%给上上级
                        $parent_p = Db::name('user')->field('id,freeze_gold,parent_user_id')->where(['freeze_gold' => ['>', 0]])->find($parent['parent_user_id']);
                        $shouyi_p_p = $userList1[$i]['fixed_gold'] * 50 / 100;//收益
                        if ($parent_p['freeze_gold'] > $shouyi_p_p) {//剩余冻结大于等于此次收益
                            Db::name('user')->where(['id' => $parent_p['id']])->setDec('freeze_gold', $shouyi_p_p);//冻结减少
                        } else {
                            $shouyi_p_p = $parent_p['freeze_gold'];//剩余冻结小于此次收益,此次收益=剩余冻结
                            Db::name('user')->where(['id' => $parent_p['id']])->setField('freeze_gold', 0);//冻结券清零
                        }
                        Db::name('user')->where(['id' => $parent_p['id']])->setInc('thaw_gold', $shouyi_p_p);//冻结券剩余增加至解冻券
                        //记录日志
                        GoldLog::create(['user_id' => $parent_p['id'], 'gold' => $shouyi_p_p, 'memo' => '间接推荐奖励', 'type' => 'thaw', 'create_at' => date('Y-m-d')]);
                    }
                }

            }
        }
        $userList1 = Db::name('user')->where(['fixed_gold' => ['>', 0], 'freeze_gold' => ['>', 0]])->field('id,fixed_gold,freeze_gold,parent_user_id,team_consume,total_consume,level')->select();
        $this->team($userList1);
        /*** 这里写计划任务列表集 END ***/
        $output->writeln('Date Crontab job end...');
    }

    public function team($userList)
    {

        $team_config = json_decode(Config::get(['name' => 'team_config'])['value'], true);
        //$userList = Db::name('user')->field('id,parent_user_id,total_consume,freeze_gold,thaw_gold,fixed_gold')->select();
        if ($team_config) {
            foreach ($team_config as $k => $v) {
                $t_config[] = ['team_consume' => $k, 'commission' => $v / 10000];
            }
            for ($i = 0; $i < count($userList); $i++) {
                $userList[$i]['is_pj'] = 0;
                $userList[$i]['real_team_consume'] = $userList[$i]['team_consume'];//-$userList[$i]['total_consume'];//无上级情况下实际团队销售额就是自身销售额
                if ($userList[$i]['parent_user_id']) {
                    //存在上级,判断两者销售额
                    foreach ($userList as $k => $v) {
                        //V是上级,判断自己销售额是否大于团队奖最大限额
                        if ($v['id'] == $userList[$i]['parent_user_id']) {
                            //找到上级
                            if ($userList[$i]['real_team_consume'] >= 100000) {
                                //与父级等级相同为平级,否则团队
                                if ($userList[$i]['level'] == $v['level']) {
                                    //$v['team_consume'] -= $userList[$i]['team_consume'];
                                    //该情况下,实际团队销售额为上级减去自己
                                    $v['real_team_consume'] = $v['team_consume'] - $userList[$i]['real_team_consume'] - $userList[$i]['total_consume'];
                                    $userList[$i]['is_pj'] = 1;
                                    $userList[$k] = $v;
                                }

                            }
                        }
                    }
                }
            }
//            var_dump($userList);
//            exit();
            //直接再次循环判断是否有团队奖即可
            for ($a = 0; $a < count($userList); $a++) {
                if ($userList[$a]['freeze_gold'] > 0) {//还有冻结券
                    $shouyi = 0;
                    for ($b = 0; $b < count($t_config); $b++) {
                        if ($b == count($t_config) - 1) {
                            if ($userList[$a]['team_consume'] >= $t_config[$b]['team_consume']) {
                                $shouyi = $userList[$a]['real_team_consume'] * $t_config[$b]['commission'];
                            }
                        } else {
                            if ($userList[$a]['team_consume'] >= $t_config[$b]['team_consume'] && $userList[$a]['team_consume'] < $t_config[$b + 1]['team_consume']) {
                                $shouyi = $userList[$a]['real_team_consume'] * $t_config[$b]['commission'];
                            }
                        }

                    }
//                    if ($userList[$a]['team_consume']>=100000 && $userList[$a]['team_consume']<500000){
//                        //超过团队奖最小销售额,获取奖励
//                        $shouyi=$userList[$a]['team_consume']*19/10000;//万分之19
//                    }elseif($userList[$a]['team_consume']>=500000 && $userList[$a]['team_consume']<1500000){
//                        $shouyi=$userList[$a]['team_consume']*29/10000;//万分之19
//                    }elseif($userList[$a]['team_consume']>=1500000 && $userList[$a]['team_consume']<3000000){
//                        $shouyi=$userList[$a]['team_consume']*39/10000;//万分之19
//                    }elseif($userList[$a]['team_consume']>=3000000 && $userList[$a]['team_consume']<6000000){
//                        $shouyi=$userList[$a]['team_consume']*49/10000;//万分之19
//                    }elseif($userList[$a]['team_consume']>=6000000){
//                        $shouyi=$userList[$a]['team_consume']*59/10000;//万分之19
//                    }
                    if ($shouyi > 0) {

                        if ($userList[$a]['freeze_gold'] >= $shouyi) {//冻结大于收益
                            Db::name('user')->where(['id' => $userList[$a]['id']])->setDec('freeze_gold', $shouyi);//冻结减
                        } else {
                            $shouyi = $userList[$a]['freeze_gold'];
                            Db::name('user')->where(['id' => $userList[$a]['id']])->setField('freeze_gold', 0);//冻结券清零
                        }
                        Db::name('user')->where(['id' => $userList[$a]['id']])->setInc('thaw_gold', $shouyi);//冻结券剩余增加至解冻券
                        //记录日志
                        GoldLog::create(['user_id' => $userList[$a]['id'], 'gold' => $shouyi, 'memo' => '团队收益', 'type' => 'thaw', 'create_at' => date('Y-m-d')]);
                        $shouyi_pj = 0;
                        if ($userList[$a]['is_pj']) {
                            //与上级是平级关系,奖收益19%给上级
                            //十分钟内所有收益,后期改为1天
                            $sy = GoldLog::where(['type' => 'thaw', 'createtime' => ['>=', time() - (3600 * 24)], 'user_id' => $userList[$a]['id'], 'pj' => 0])->sum('gold');
                            $shouyi_pj = $sy * 19 / 100;
                        }
                        if ($shouyi_pj && $userList[$a]['parent_user_id']) {
                            $parent = Db::name('user')->field('id,freeze_gold,parent_user_id')->where(['freeze_gold' => ['>', 0]])->find($userList[$a]['parent_user_id']);
                            if ($parent && $parent['freeze_gold'] >= $shouyi_pj) {
                                Db::name('user')->where(['id' => $parent['id']])->setDec('freeze_gold', $shouyi_pj);//冻结减少
                            } else {
                                $shouyi_pj = $parent['freeze_gold'];
                                Db::name('user')->where(['id' => $parent['id']])->setField('freeze_gold', 0);//冻结券清零
                            }
                            Db::name('user')->where(['id' => $parent['id']])->setInc('thaw_gold', $shouyi_pj);//冻结券剩余增加至解冻券
                            //记录日志
                            GoldLog::create(['user_id' => $parent['id'], 'gold' => $shouyi_pj, 'memo' => '平级奖励', 'type' => 'thaw', 'create_at' => date('Y-m-d')]);
                            GoldLog::where(['type' => 'thaw', 'createtime' => ['>=', time() - 600], 'user_id' => $userList[$a]['id'], 'pj' => 0])->setField('pj', 1);
                        }
                    }

                }
            }
        }

    }
}
